// Eternal Lands Bot
// Copyright (C) 2006  Artem Makhutov
// artem@makhutov.org
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

using System;

namespace cs_elbot.AdvancedCommunication
{
	/// <summary>
	/// description of BuddyHandler.
	/// </summary>
	public class BuddyHandler
	{
		private TCPWrapper TheTCPWrapper;
		private BasicCommunication.MessageParser TheMessageParser;
		private MySqlManager TheMySqlManager;
		////private bool CommandIsDisabled;
		private AdvHelpCommandHandler TheAdvHelpCommandHandler;
		private System.Collections.Hashtable BuddyList = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
		
		public struct Buddy
		{
			public string name;
			public System.DateTime LoginTime;
			public System.DateTime LogoutTime;
			public bool Online;
		}
		
		public BuddyHandler(TCPWrapper MyTCPWrapper, BasicCommunication.MessageParser MyMessageParser, AdvHelpCommandHandler MyAdvHelpCommandHandler ,MySqlManager MyMySqlManager)
		{
			this.TheTCPWrapper = MyTCPWrapper;
			this.TheMessageParser = MyMessageParser;
			this.TheAdvHelpCommandHandler = MyAdvHelpCommandHandler;
			this.TheMySqlManager = MyMySqlManager;
			this.TheTCPWrapper.GotCommand += new TCPWrapper.GotCommandEventHandler(OnGotCommand);
			
			//this.CommandIsDisabled = MyMySqlManager.CheckIfCommandIsDisabled("#online",Settings.botid);
			
			//if (CommandIsDisabled == false)
			{
				TheAdvHelpCommandHandler.AddCommand("#online - lists online buddies");
				TheMessageParser.Got_PM += new BasicCommunication.MessageParser.Got_PM_EventHandler(OnGotPM);
			}
		}
		
		private void OnGotCommand(object sender, TCPWrapper.GotCommandEventArgs e)
		{
			if (e.CommandBuffer[0] == 59 && e.CommandBuffer[3] == 1)
			{
				string name = System.Text.ASCIIEncoding.ASCII.GetString(e.CommandBuffer, 5, e.CommandBuffer.Length-6);
				
				Buddy MyBuddy;
				
				if (BuddyList.Contains(name.ToLower()))
			    {
					MyBuddy = (Buddy)BuddyList[name.ToLower()];
			    	MyBuddy.LoginTime = System.DateTime.Now;
					MyBuddy.Online = true;
					BuddyList[name.ToLower()] = MyBuddy;
			    }
				else
				{
					MyBuddy = new Buddy();
					MyBuddy.name = name.ToLower();
					MyBuddy.LoginTime = System.DateTime.Now;
					MyBuddy.Online = true;
					BuddyList.Add(name.ToLower(),MyBuddy);
				}

                TheTCPWrapper.Send(CommandCreator.RAW_TEXT("#gm " + System.Convert.ToChar(TheMySqlManager.playerloggedoncolor(Settings.botid)) + "### " + name.ToUpper() + " HAS LOGGED ON ###"));
			}
			
			if (e.CommandBuffer[0] == 59 && e.CommandBuffer[3] == 0)
			{
				string name = System.Text.ASCIIEncoding.ASCII.GetString(e.CommandBuffer, 4, e.CommandBuffer.Length-5);
				
				Buddy MyBuddy;
				
				if (BuddyList.Contains(name.ToLower()))
			    {
					MyBuddy = (Buddy)BuddyList[name.ToLower()];
			    	MyBuddy.LogoutTime = System.DateTime.Now;
					MyBuddy.Online = false;
					BuddyList[name.ToLower()] = MyBuddy;
			    }
				else
				{
					MyBuddy = new Buddy();
					MyBuddy.name = name.ToLower();
					MyBuddy.LogoutTime = System.DateTime.Now;
					MyBuddy.Online = false;
					BuddyList.Add(name.ToLower(),MyBuddy);
				}

                TheTCPWrapper.Send(CommandCreator.RAW_TEXT("#gm " + System.Convert.ToChar(TheMySqlManager.playerloggedoffcolor(Settings.botid)) + "### " + name.ToUpper() + " HAS LOGGED OFF ###"));
			}
		}
		
		private void OnGotPM(object sender, BasicCommunication.MessageParser.Got_PM_EventArgs e)
		{
			string Message = e.Message.ToLower().Replace("\'","\\\'").Replace("\"","\\\"");
			
			if (Message[0]!='#')
			{
				Message = "#" + Message;
			}
			
			string[] CommandArray = Message.Split(' ');
			
			if (CommandArray[0] == "#online")
			{
				if (TheMySqlManager.GetUserRank(e.username,Settings.botid)<TheMySqlManager.GetCommandRank("#online",Settings.botid))
			    {
			    	TheTCPWrapper.Send(CommandCreator.SEND_PM(e.username,"You are not authorized to use this command!"));
			    	return;
			    }
				
				if (CommandArray.Length<1)
					goto WrongArguments;
				
				TheTCPWrapper.Send(CommandCreator.SEND_PM(e.username,"Buddylist monitoring - name | Lastlogin | Lastlogout:"));
				TheTCPWrapper.Send(CommandCreator.SEND_PM(e.username,"Online buddies:"));
				
				string msg="";
				
				lock (BuddyList.SyncRoot)
				{
					foreach ( Buddy MyBuddy in BuddyList.Values)
					{
						if (MyBuddy.Online==true)
						{
							msg=msg.PadRight(150,' ');
						
							msg = msg.Insert(0,MyBuddy.name);
							msg = msg.Insert(12,"|");
							msg = msg.Insert(14,MyBuddy.LoginTime.ToUniversalTime().ToString());
							msg = msg.Insert(34,"|");
							msg = msg.Insert(36,MyBuddy.LogoutTime.ToUniversalTime().ToString());
							msg = msg.TrimEnd();
							TheTCPWrapper.Send(CommandCreator.SEND_PM(e.username, msg));
							msg="";
						}
					}
					TheTCPWrapper.Send(CommandCreator.SEND_PM(e.username,"Offline buddies:"));
					foreach ( Buddy MyBuddy in BuddyList.Values)
					{
						if (MyBuddy.Online==false)
						{
							msg=msg.PadRight(150,' ');
						
							msg = msg.Insert(0,MyBuddy.name);
							msg = msg.Insert(12,"|");
							msg = msg.Insert(14,MyBuddy.LoginTime.ToUniversalTime().ToString());
							msg = msg.Insert(34,"|");
							msg = msg.Insert(36,MyBuddy.LogoutTime.ToUniversalTime().ToString());
							msg = msg.TrimEnd();
							TheTCPWrapper.Send(CommandCreator.SEND_PM(e.username, msg));
							msg="";
						}
					}
				}
			}
				
			
			return;
					
			WrongArguments:
				TheTCPWrapper.Send(CommandCreator.SEND_PM(e.username,"Here is the usage of the #online command:"));
				TheTCPWrapper.Send(CommandCreator.SEND_PM(e.username,"#online"));
				TheTCPWrapper.Send(CommandCreator.SEND_PM(e.username,"#online all"));
				TheTCPWrapper.Send(CommandCreator.SEND_PM(e.username,"------------------------------------------"));
				TheTCPWrapper.Send(CommandCreator.SEND_PM(e.username,"Example: #online"));
				TheTCPWrapper.Send(CommandCreator.SEND_PM(e.username,"Example: #online all"));
				return;
		}
	}
}
